﻿using GrapeCity.Forguncy.CellTypes;
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Vant.Properties;
using Vant.WpfControls.DrawingObject;

namespace Vant.CellTypes
{
    public class StepperDesigner : CellTypeDesigner<Stepper>, ISupportPropertyInitialize
    {
        public override FrameworkElement GetDrawingControl(ICellInfo cellInfo, IDrawingHelper drawingHelper)
        {
            return new StepperDrawingObject(this.CellType, drawingHelper, cellInfo);
        }
        public void InitDefaultPropertyValues(IBuilderContext context)
        {
            this.CellType.min = 0;
            this.CellType.step = 1;
            this.CellType.theme = "rec";
            this.CellType.buttonSize = 28;
            this.CellType.decimalLength = 0;
        }
    }

    [Designer("Vant.CellTypes.StepperDesigner, Vant")]
    [Icon("pack://application:,,,/Vant;component/Resources/Images/Stepper.png")]
    [OrderWeight((int)Utils.OrderWeight.Stepper)]
    public class Stepper : VantCellTypeBase, ICommandCellType, ISupportDataValidation, ISupportUIPermission, ISupportDefaultValue, ISupportDisable
    {
        [SRDisplayName(nameof(Resources.Vant_EditCommands))]
        public List<Command> CommandList { get; set; } = new List<Command>();
        public CommandExcuteKind CommandExcuteKind => CommandExcuteKind.OnValueChanged;

        [SRDisplayName(nameof(Resources.Vant_DataValidation))]
        [JsonIgnore]
        [DefaultValue(null)]
        public DataValidationLink DataValidationLink { get; set; }

        List<UIPermission> _uIPermissions;
        [SRDisplayName(nameof(Resources.Vant_UIPermissions))]
        [JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace)]
        public List<UIPermission> UIPermissions
        {
            get
            {
                if (_uIPermissions == null)
                {
                    _uIPermissions = Utils.GetDefaultPermission(UIPermissionScope.Enable, UIPermissionScope.Visible);
                }
                return _uIPermissions;
            }
            set
            {
                _uIPermissions = value;
            }
        }

        [FormulaProperty]
        [SRDisplayName(nameof(Resources.Vant_DefaultValue))]
        public object DefaultValue { get; set; }

        [Browsable(false)]
        [JsonIgnore]
        public bool NeedFormatDefaultValue => true;

        [FormulaProperty]
        [SRDisplayName(nameof(Resources.Stepper_Min))]
        public object min { get; set; }

        [FormulaProperty]
        [SRDisplayName(nameof(Resources.Stepper_Max))]
        public object max { get; set; }

        [FormulaProperty]
        [SRDisplayName(nameof(Resources.Stepper_Step))]
        public object step { get; set; }

        [IntProperty(Min = 0)]
        [SRDisplayName(nameof(Resources.Stepper_DecimalLength))]
        public int decimalLength { get; set; }

        [SRDisplayName(nameof(Resources.Stepper_Placeholder))]
        public string placeholder { get; set; }

        [SRComboProperty(ValueList = "rec|round", DisplayList = nameof(Resources.Stepper_Theme_DisplayList))]
        [SRDisplayName(nameof(Resources.Stepper_Theme))]
        public string theme { get; set; }

        [IntProperty(Min = 0)]
        [SRDisplayName(nameof(Resources.Stepper_ButtonSize))]
        public int buttonSize { get; set; }

        [SRCategoryHeader(nameof(Resources.Other))]
        [SRDisplayName(nameof(Resources.Stepper_Integer))]
        [DefaultValue(false)]
        public bool integer { get; set; } = false;

        [SRDisplayName(nameof(Resources.Stepper_AllowEmpty))]
        [DefaultValue(true)]
        public bool allowEmpty { get; set; } = true;

        [SRDisplayName(nameof(Resources.Stepper_ShowInput))]
        [DefaultValue(true)]
        public bool showInput { get; set; } = true;

        [SRDisplayName(nameof(Resources.Stepper_ShowPlus))]
        [DefaultValue(true)]
        public bool showPlus { get; set; } = true;

        [SRDisplayName(nameof(Resources.Stepper_ShowMinus))]
        [DefaultValue(true)]
        public bool showMinus { get; set; } = true;

        [SRDisplayName(nameof(Resources.Vant_Disable))]
        [DefaultValue(false)]
        public bool IsDisabled { get; set; } = false;

        [SRDisplayName(nameof(Resources.Stepper_DisableInput))]
        [DefaultValue(false)]
        public bool disableInput { get; set; } = false;

        [SRDisplayName(nameof(Resources.Stepper_LongPress))]
        [DefaultValue(true)]
        public bool longPress { get; set; } = true;

        public override SupportFeatures SupportFeatures
        {
            get
            {
                return SupportFeatures.ShouldCheckDirtyWhenLeavePage;
            }
        }

        public override string ToString()
        {
            return Resources.Stepper_DisplayName;
        }

        [RunTimeMethod]
        [SRDisplayName(nameof(Resources.Stepper_SetMin))]
        public void SetMinValue([Required][SRItemDisplayName(nameof(Resources.Stepper_Min))] int min)
        {
            //
        }

        [RunTimeMethod]
        [SRDisplayName(nameof(Resources.Stepper_SetMax))]
        public void SetMaxValue([Required][SRItemDisplayName(nameof(Resources.Stepper_Max))] int max)
        {
            //
        }

        [RunTimeMethod]
        [SRDisplayName(nameof(Resources.Stepper_SetStep))]
        public void SetStepSize([Required][SRItemDisplayName(nameof(Resources.Stepper_Step))] int step)
        {
            //
        }

        [RunTimeMethod]
        [SRDisplayName(nameof(Resources.Stepper_SetFocus))]
        public void SetFocus()
        {
            //
        }
    }
}
